// pages/home/home.js
const app = getApp();
const Util = require("../../utils/util.js");

Page({
    
    data: {
        groupRange: ["小学", "初中", "高中"],
        gradeRange: [],
        subjectRange: ["语文", "数学", "英语"],
        searchBarIcon: "/assets/glass.png",
        searchResult: [],
        examList: [],
        displaySearchResult: false,
        displayExamList: false
    },
    
    onLoad: function (options) {
        this.grade = '';
        this.group = '';
        this.subject = '';
    },

    onShow: function () {
        this.getTabBar().setData({
            selected: 0
        })
    },

    onConfirm: async function (e) {
        let b = app.globalData.baseURL;
        let resp = await Util.asyncHttpPost(b + 'findQuestion', {
            name: e.detail.value
        });
        console.log(resp.data);
        this.setData({
            displaySearchResult: true,
            searchResult: resp.data.data
        })
    },

    tapSearchResultItem: function (e) {
        let index = parseInt(e.currentTarget.dataset.index);
        app.installQuestionSet([this.data.searchResult[index]]);
        wx.navigateTo({
            url: "/pages/problem/problem"
        })
    },

    onBlur: function (e) {
        this.setData({
            searchBarIcon: "/assets/glass.png"
        })
    },

    onFocus: function (e) {
        this.setData({
            searchBarIcon: "/assets/glass-s.png"
        })
    },

    dismissSearchResult: function (e) {
        this.setData({
            displaySearchResult: false,
            displayExamList: false
        })
    },

    changeGroup: function (e) {
        this.group = e.detail.value;
        if (this.group == '小学') {
            this.setData({
                gradeRange: ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级']
            })
        } else if (this.group == '初中') {
            this.setData({
                gradeRange: ['初一', '初二', '初三']
            })
        } else {
            this.setData({
                gradeRange: ['高一', '高二', '高三']
            })
        }
    },

    changeGrade: function (e) {
        this.grade = e.detail.value;
    },

    changeSubject: function (e) {
        this.subject = e.detail.value;
    },

    getExam: async function () {
        let b = app.globalData.baseURL;
        let res = await Util.asyncHttpPost(b + 'findExamList', {
            gradeName: this.grade,
            groupName: this.group,
            subject: this.subject,
            tenantId: "757021031830388736"
        });
        this.setData({
            displaySearchResult: false,
            displayExamList: true,
            examList: res.data.data
        })
    },

    gotoExam: function (e) {
        this.getExam();
    },

    goPractise: async function (e) {
        let b = app.globalData.baseURL;
        let resp = await Util.asyncHttpPost(b + 'randomQuestion', {
            gradeName: this.grade,
            subject: this.subject,
            groupName: this.group,
            tenantId: "757021031830388736"
        });
        let question = resp.data.data;
        app.installQuestionSet([question]);
        app.globalData.exerciseMode = true;
        app.globalData.gradeName = this.grade;
        app.globalData.groupName = this.group;
        app.globalData.subject = this.subject;
        app.globalData.tenantId = "757021031830388736";
        wx.navigateTo({
            url: "/pages/problem/problem"
        });
    },

    tapExamListItem: async function (e) {
        let index = parseInt(e.currentTarget.dataset.index);
        let exam = this.data.examList[index];
        let b = app.globalData.baseURL + `examById?paperId=${exam.id}`;
        let resp = await Util.asyncHttpGet(b);
        let problems = resp.data.data;
        if (problems.length == 0) {
            wx.showModal({
                title: "试卷是空的。"
            });
            return;
        }
        app.installQuestionSet(problems);
        app.globalData.exerciseMode = false;
        wx.navigateTo({
            url: "/pages/problem/problem"
        })
    }
})